package com.ttctracker.search;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.ttctracker.R;
import com.ttctracker.classes.StopItem;
import com.ttctracker.xmlparser.SAXParserHelper;
import com.ttctracker.xmlparser.StopPredictionHandler;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.text.Html;
import android.widget.TextView;

public class RefreshPredictionTask extends AsyncTask<String, Void, StopPredictionHandler>{
	
	private Dialog predictiondDialog;
	private ProgressDialog progressDialog;
	
	/**
	 * 
	 */
	public RefreshPredictionTask(Dialog predictionDialog) {
		this.predictiondDialog=predictionDialog;
	}

	@Override
	protected void onPostExecute(StopPredictionHandler result) {
		//progressDialog.dismiss();
		if (result!=null) {
			StopItem resultStopItem=result.getStopItem();
			ArrayList<Integer> predictions=resultStopItem.get_prediction();
			TextView predictionText=(TextView)predictiondDialog.findViewById(R.id.prediction_dialog_predictions);
			predictionText.setText(Html.fromHtml(getPredictionContent(predictions)));
		}
	}

	private String getPredictionContent(ArrayList<Integer> predictions){
		String dialogBodyText = "";
		int stopCounter = 1;
		for (Integer prediction : predictions) {
			if (stopCounter > 3) {
				break;
			} else {
				if (prediction >= 0) {

					String prediction_txt = Integer.toString(prediction);

					if (stopCounter == 1) {
						dialogBodyText += "<font color=\"red\"><b>";
						if (prediction != 0) {
							dialogBodyText += prediction_txt
									+ " Min</b></font><br/>";
						} else {
							dialogBodyText += "Due</b></font><br/>";
						}

					} else {
						dialogBodyText += "" + prediction_txt + " Min<br/>";
					}

				} else {
					dialogBodyText += "N/A";
					break;
				}
				stopCounter++;
			}

		}
		return dialogBodyText;
	}
	
	@Override
	protected void onPreExecute() {
		progressDialog = new ProgressDialog(predictiondDialog.getContext());
		progressDialog.setMessage("Refreshing prediction...");
		//progressDialog.show();
	}

	@Override
	protected StopPredictionHandler doInBackground(String... arg0) {
		InputSource iSource;
		try {
			
			iSource = new InputSource();
			SAXParserHelper helper = new SAXParserHelper(new URL(arg0[0]).openStream(),
					StopPredictionHandler.class);

			// StopPredictionHandler handler = new StopPredictionHandler();

			helper.setup_parser();
			helper.parseXml();
			return ((StopPredictionHandler) helper.getHandler());

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

}
